package com.markerhub.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.markerhub.common.dto.ClassUpdateDto;
import com.markerhub.common.dto.CourseUpdateDto;
import com.markerhub.common.lang.Result;
import com.markerhub.entity.*;
import com.markerhub.service.ManagerClassService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/manager-class")
public class ManagerClassController {
    @Autowired
    ManagerClassService classService;


    @GetMapping("/teacher-classes")
    public Result getTeacherClasses(
            @RequestParam Long teacherId,
            @RequestParam(required = false) String className,
            @RequestParam(defaultValue = "1") Integer currentPage,
            @RequestParam(defaultValue = "10") Integer pageSize) {

        Page<ManagerClass> page = new Page<>(currentPage, pageSize);
        IPage<ManagerClass> pageData = classService.getTeacherClasses(page, teacherId, className);
        return Result.succ(pageData);
    }

    @GetMapping("/students/{classId}")
    public Result getClassStudents(@PathVariable Integer classId) {
        List<ManagerStudent> students = classService.getClassStudents(classId);
        return Result.succ(students);
    }




    @RequiresRoles("admin")
    @RequiresAuthentication
    @DeleteMapping("/delete/{id}")
    public Result deleteCourse(@PathVariable Long id) {
        boolean isDeleted = classService.removeById(id);
        if (isDeleted) {
            return Result.succ("教学班删除成功");
        } else {
            return Result.fail("教学班删除失败，课程不存在");
        }
    }

    @RequiresAuthentication
    @GetMapping("/class/{id}")
    public Result selectById(@PathVariable(name = "id") Long id) {
        ManagerClass class1 = classService.getById(id);
        Assert.notNull(class1, "该class已被删除");

        return Result.succ(class1);
    }

    @RequiresAuthentication
    @GetMapping("/all")
    public Result getAllActivities() {
        List<ManagerClass> classes = classService.list(); // 获取所有活动
        return Result.succ(classes);
    }
    @RequiresAuthentication
    @GetMapping("/select")
    public Result getClasses(
            @RequestParam(value = "activityName", required = false) String activityName,
            @RequestParam(value = "className", required = false) String className,
            @RequestParam(value = "classDesc", required = false) String classDesc,
            @RequestParam(value = "teacherName", required = false) String teacherName,
            @RequestParam(value = "currentPage", defaultValue = "1") int currentPage,
            @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {

        IPage<ManagerClass> classes = classService.getClasses(activityName, className, classDesc, teacherName, currentPage, pageSize);
        return Result.succ(classes);
    }
    // Controller 层代码
    //@RequiresAuthentication
    @GetMapping("/select1")
    public Result getClassesByActivityName(
            @RequestParam(value = "activityName", required = false) String activityName,
            @RequestParam(value = "currentPage", defaultValue = "1") int currentPage,
            @RequestParam(value = "pageSize", defaultValue = "5") int pageSize) {

        // 调用Service查询
        IPage classes = classService.getClassesByActivityName(activityName, currentPage, pageSize);

        // 返回查询结果
        return Result.succ(classes);
    }

    @RequiresAuthentication
    @GetMapping("/classes")
    public Result list(@RequestParam(defaultValue = "1") Integer currentPage) {

        Page page = new Page(currentPage, 5);
        IPage pageData = classService.page(page, new QueryWrapper<ManagerClass>().orderByDesc("created_time"));
        return Result.succ(pageData);
    }



    @RequiresAuthentication
    @PostMapping("/update")
    public Result saveOrUpdateCourse(@RequestBody ClassUpdateDto classUpdateDto) {


        // 2. 更新课程信息
        ManagerClass class1 = new ManagerClass();
        if (classUpdateDto.getId() != null) {
            class1.setId(classUpdateDto.getId()); // 设置课程ID用于更新
        }
        class1.setName(classUpdateDto.getName());
        class1.setClassDesc(classUpdateDto.getClassDesc());
        class1.setActivityId(classUpdateDto.getActivityId());
        class1.setTeacherId(classUpdateDto.getTeacherId());

        // 3. 保存课程信息
        classService.saveOrUpdate(class1);

        return Result.succ("课程保存成功");
    }

}
